<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../Universal.css">
</head>
<body>
    
    <div id="cnblogs_post_body" class="blogpost-body">
        <p>什么是异常？Java代码在运行时期发生的问题就是异常。</p>
    <p>在Java中，把异常信息封装成了一个类。当出现了问题时，就会创建异常类对象并抛出异常相关的信息（如异常出现的位置、原因等）。</p>
    <h3>异常的继承体系</h3>
    <p>在Java中使用Exception类来描述异常。</p>
    <p>查看API中Exception的描述，Exception 类及其子类是 Throwable 的一种形式，它用来表示java程序中可能会产生的异常，并要求对产生的异常进行合理的异常处理。</p>
    <p>Exception有继承关系，它的父类是Throwable。Throwable是Java 语言中所有错误或异常的超类，即祖宗类。&nbsp;</p>
    <p>另外，在异常Exception类中，有一个子类要特殊说明一下，RuntimeException子类，RuntimeException及其它的子类只能在Java程序运行过程中出现。</p>
    <p>我们再来观察Throwable类，能够发现与异常Exception平级的有一个Error，它是Throwable的子类，它用来表示java程序中可能会产生的严重错误。解决办法只有一个，修改代码避免Error错误的产生。</p>
    <p>&nbsp;<img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170715221854853-160305026.png" alt="">&nbsp;<img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170715221909837-104761646.png" alt="">&nbsp;</p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170715222144806-1069994273.png" alt="">&nbsp;</p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170715222159259-350207852.png" alt=""></p>
    <p><span style="line-height: 1.5">异常继承体系总结：</span></p>
    <ul>
    <li>Throwable: 它是所有错误与异常的超类（祖宗类）</li>
    <li>Error 错误</li>
    <li>Exception 编译期异常,进行编译JAVA程序时出现的问题</li>
    <li>RuntimeException 运行期异常, JAVA程序运行过程中出现的问题</li>
    </ul>
    <p>异常与错误的区别</p>
    <p>异常：指程序在编译、运行期间发生了某种异常(XxxException)，我们可以对异常进行具体的处理。若不处理异常，程序将会结束运行。</p>
    <p>异常的产生演示如下：</p>
    <div class="cnblogs_code">
    <pre>  <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">];
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(arr[<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">]);
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(arr[<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">]);
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 该句运行时发生了数组索引越界异常ArrayIndexOutOfBoundsException，由于没有处理异常，导致程序无法继续执行，程序结束。</span>
            System.<span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">over</span><span style="color: rgba(128, 0, 0, 1)">"</span>); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 由于上面代码发生了异常，此句代码不会执行</span>
        }</pre>
    </div>
    <p><span style="line-height: 1.5">错误：指程序在运行期间发生了某种错误(XxxError)，Error错误通常没有具体的处理方式，程序将会结束运行。Error错误的发生往往都是系统级别的问题，都是jvm所在系统发生的，并反馈给jvm的。我们无法针对处理，只能修正代码。</span></p>
    <p>错误的产生演示如下：</p>
    <p>&nbsp;</p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[<span style="color: rgba(128, 0, 128, 1)">1024</span>*<span style="color: rgba(128, 0, 128, 1)">1024</span>*<span style="color: rgba(128, 0, 128, 1)">100</span><span style="color: rgba(0, 0, 0, 1)">];
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">该句运行时发生了内存溢出错误OutOfMemoryError，开辟了过大的数组空间，导致JVM在分配数组空间时超出了JVM内存空间，直接发生错误。</span>
        }</pre>
    </div>
    <p><span style="line-height: 1.5">异常的产生过程解析</span></p>
    <p>先运行下面的程序，程序会产生一个数组索引越界异常ArrayIndexOfBoundsException。我们通过图解来解析下异常产生的过程。</p>
    <p>工具类</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayTools{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">对给定的数组通过给定的角标获取元素。</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> getElement(<span style="color: rgba(0, 0, 255, 1)">int</span>[] arr,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> index) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> element =<span style="color: rgba(0, 0, 0, 1)"> arr[index];
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> element;
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">测试类</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ExceptionDemo2 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = {<span style="color: rgba(128, 0, 128, 1)">34</span>,<span style="color: rgba(128, 0, 128, 1)">12</span>,<span style="color: rgba(128, 0, 128, 1)">67</span><span style="color: rgba(0, 0, 0, 1)">};
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> num = ArrayTools.getElement(arr,<span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)">)
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">num=</span><span style="color: rgba(128, 0, 0, 1)">"</span>+<span style="color: rgba(0, 0, 0, 1)">num);
            System.</span><span style="color: rgba(0, 0, 255, 1)">out</span>.println(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">over</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">上述程序执行过程图解：</span></p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170715222450556-1862180831.png" alt=""></p>
    <h3><span style="line-height: 1.5">抛出异常throw</span></h3>
    <p>在编写程序时，我们必须要考虑程序出现问题的情况。比如，在定义方法时，方法需要接受参数。那么，当调用方法使用接受到的参数时，首先需要先对参数数据进行合法的判断，数据若不合法，就应该告诉调用者，传递合法的数据进来。这时需要使用抛出异常的方式来告诉调用者。</p>
    <p>在java中，提供了一个throw关键字，它用来抛出一个指定的异常对象。那么，抛出一个异常具体如何操作呢？</p>
    <p>1、创建一个异常对象。封装一些提示信息(信息可以自己编写)。</p>
    <p>2、需要将这个异常对象告知给调用者。怎么告知呢？怎么将这个异常对象传递到调用者处呢？通过关键字throw就可以完成。throw 异常对象；throw用在方法内，用来抛出一个异常对象，将这个异常对象传递到调用者处，并结束当前方法的执行。</p>
    <p>使用格式：&nbsp;<span class="cnblogs_code"><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> 异常类名(参数);</span>&nbsp;</p>
    <p>例如：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> NullPointerException("要访问的arr数组不存在"<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayIndexOutOfBoundsException("该索引在数组中不存在，已超出范围");</pre>
    </div>
    <p>下面是异常类ArrayIndexOutOfBoundsException与NullPointerException的构造方法</p>
    <p><img src="https://images2015.cnblogs.com/blog/612774/201707/612774-20170715222730618-835656632.png" alt=""></p>
    <p><span style="line-height: 1.5">学习完抛出异常的格式后，我们通过下面程序演示下throw的使用。</span></p>
    <p>编写工具类，提供获取数组指定索引处的元素值</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ArrayTools{
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过给定的数组，返回给定的索引对应的元素值。</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> getElement(<span style="color: rgba(0, 0, 255, 1)">int</span>[] arr,<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> index) {
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    若程序出了异常，JVM它会打包异常对象并抛出。但是它所提供的信息不够给力。想要更清晰，需要自己抛出异常信息。
    下面判断条件如果满足，当执行完throw抛出异常对象后，方法已经无法继续运算。这时就会结束当前方法的执行，并将异常告知给调用者。这时就需要通过异常来解决。
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span>(arr==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> NullPointerException("arr指向的数组不存在"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(index&lt;0 || index&gt;=<span style="color: rgba(0, 0, 0, 1)">arr.length){
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayIndexOutOfBoundsException("错误的角标，"+index+"索引在数组中不存在"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> element =<span style="color: rgba(0, 0, 0, 1)"> arr[index];
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> element;
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">测试类</span>&nbsp;</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ExceptionDemo3 {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = {34,12,67}; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建数组</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span> num = ArrayTools.getElement(<span style="color: rgba(0, 0, 255, 1)">null</span>,2);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用方法，获取数组中指定索引处元素
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">int num = ArrayTools.getElement(arr,5);</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 调用方法，获取数组中指定索引处元素</span>
            System.out.println("num="+num);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">打印获取到的元素值</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    }</span></pre>
    </div>
    <h3><span style="line-height: 1.5">&nbsp;声明异常throws</span></h3>
    <p>声明：将问题标识出来，报告给调用者。如果方法内通过throw抛出了编译时异常，而没有捕获处理（稍后讲解该方式），那么必须通过throws进行声明，让调用者去处理。</p>
    <p>声明异常格式：&nbsp;<span class="cnblogs_code">修饰符 返回值类型 方法名(参数) <span style="color: rgba(0, 0, 255, 1)">throws</span> 异常类名1,异常类名2… { }</span>&nbsp;</p>
    <p>声明异常的代码演示：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Demo{
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    如果定义功能时有问题发生需要报告给调用者。可以通过在方法上使用throws关键字进行声明。
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> show(<span style="color: rgba(0, 0, 255, 1)">int</span> x)<span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(x&gt;0<span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Exception();
            } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
                System.out.println(</span>"show run"<span style="color: rgba(0, 0, 0, 1)">);
            }
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">throws用于进行异常类的声明，若该方法可能有多种异常情况产生，那么在throws后面可以写多个异常类，用逗号隔开。</span></p>
    <p>多个异常的情况，例如:</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">int</span> getElement(<span style="color: rgba(0, 0, 255, 1)">int</span>[] arr,<span style="color: rgba(0, 0, 255, 1)">int</span> index) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> NullPointerException, ArrayIndexOutOfBoundsException {
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(arr==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> NullPointerException("arr指向的数组不存在"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(index&lt;0 || index&gt;=<span style="color: rgba(0, 0, 0, 1)">arr.length){
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayIndexOutOfBoundsException("错误的角标，"+index+"索引在数组中不存在"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> element =<span style="color: rgba(0, 0, 0, 1)"> arr[index];
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> element;
        }</span></pre>
    </div>
    <h3><span style="line-height: 1.5">捕获异常try…catch…finally</span></h3>
    <p>捕获：Java中对异常有针对性的语句进行捕获，可以对出现的异常进行指定方式的处理</p>
    <p>捕获异常格式：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
    　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需要被检测的语句。</span><span style="color: rgba(0, 0, 0, 1)">
    }
    </span><span style="color: rgba(0, 0, 255, 1)">catch</span>(异常类 变量) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">参数。
    　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">异常的处理语句。</span><span style="color: rgba(0, 0, 0, 1)">
    }
    </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
    　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">一定会被执行的语句。</span><span style="color: rgba(0, 0, 0, 1)">
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">try：该代码块中编写可能产生异常的代码。</span></p>
    <p>catch：用来进行某种异常的捕获，实现对捕获到的异常进行处理。</p>
    <p>finally：有一些特定的代码无论异常是否发生，都需要执行。另外，因为异常会引发程序跳转，导致有些语句执行不到。而finally就是解决这个问题的，在finally代码块中存放的代码都是一定会被执行的。</p>
    <p>演示如下：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ExceptionDemo{
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">throws ArrayIndexOutOfBoundsException</span>
            <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                </span><span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = <span style="color: rgba(0, 0, 255, 1)">new</span> <span style="color: rgba(0, 0, 255, 1)">int</span>[3<span style="color: rgba(0, 0, 0, 1)">];
                System.out.println( arr[</span>5] );<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 会抛出ArrayIndexOutOfBoundsException
                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">当产生异常时，必须有处理方式。要么捕获，要么声明。</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span> (ArrayIndexOutOfBoundsException e) { <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">括号中需要定义什么呢？try中抛出的是什么异常，在括号中就定义什么异常类型。</span>
                System.out.println("异常发生了"<span style="color: rgba(0, 0, 0, 1)">);
            } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
                arr </span>= <span style="color: rgba(0, 0, 255, 1)">null</span>; <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把数组指向null，通过垃圾回收器，进行内存垃圾的清除</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
            System.out.println(</span>"程序运行结果"<span style="color: rgba(0, 0, 0, 1)">);
        }
    }</span></pre>
    </div>
    <h3><span style="line-height: 1.5">try…catch…finally异常处理的组合方式</span></h3>
    <p>try catch finally组合：检测异常，并传递给catch处理，并在finally中进行资源释放。</p>
    <p>try catch组合 : 对代码进行异常检测，并对检测的异常传递给catch处理。对异常进行捕获处理。</p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">void</span> show(){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">不用throws</span>
            <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Exception();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">产生异常，直接捕获处理</span>
            }<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理方式</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }</span></pre>
    </div>
    <p><span style="line-height: 1.5">一个try 多个catch组合 : 对代码进行异常检测，并对检测的异常传递给catch处理。对每种异常信息进行不同的捕获处理。</span></p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">void</span> show(){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">不用throws</span>
            <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Exception();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">产生异常，直接捕获处理</span>
            }<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(XxxException e){
        　　　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理方式</span>
            }<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(YyyException e){
        　　　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理方式</span>
            }<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(ZzzException e){
        　　　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">处理方式</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }</span></pre>
    </div>
    <p><span style="line-height: 1.5">注意:这种异常处理方式，要求多个catch中的异常不能相同，并且若catch中的多个异常之间有子父类异常的关系，那么子类异常要求在上面的catch处理，父类异常在下面的catch处理。</span></p>
    <p>&nbsp;try finally 组合: 对代码进行异常检测，检测到异常后因为没有catch，所以一样会被默认jvm抛出。异常是没有捕获处理的。但是功能所开启资源需要进行关闭，所有finally。只为关闭资源。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">void</span> show(){<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">需要throws</span>
            <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Exception();
            }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">释放资源</span>
    <span style="color: rgba(0, 0, 0, 1)">        }
        }</span></pre>
    </div>
    <h3><span style="line-height: 1.5">运行时期异常</span></h3>
    <p>RuntimeException和他的所有子类异常,都属于运行时期异常。NullPointerException,ArrayIndexOutOfBoundsException等都属于运行时期异常.</p>
    <h3>运行时期异常的特点</h3>
    <p>方法中抛出运行时期异常,方法定义中无需throws声明,调用者也无需处理此异常</p>
    <p>运行时期异常一旦发生,需要程序人员修改源代码.</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ExceptionDemo{
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args){
             method();
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method(){
            </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException();
        }
    }</span></pre>
    </div>
    <h3><span style="line-height: 1.5">异常在方法重写中细节</span></h3>
    <p>1、子类覆盖父类方法时，如果父类的方法声明异常，子类只能声明父类异常或者该异常的子类，或者不声明。</p>
    <p>例如：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Fu {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method () <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException {
        }
    }</span></pre>
    </div>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span> Zi <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Fu {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method() <span style="color: rgba(0, 0, 255, 1)">throws</span> RuntimeException { }  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">抛出父类一样的异常
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">public void method() throws NullPointerException{ } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">抛出父类子异常</span>
    }</pre>
    </div>
    <p><span style="line-height: 1.5">2、当父类方法声明多个异常时，子类覆盖时只能声明多个异常的子集。</span></p>
    <p>例如：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Fu {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method () <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> NullPointerException, ClassCastException{
        }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">class</span> Zi <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Fu {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method()<span style="color: rgba(0, 0, 255, 1)">throws</span> NullPointerException, ClassCastException { }   <br><span style="color: rgba(0, 0, 255, 1)">    public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method() <span style="color: rgba(0, 0, 255, 1)">throws</span> NullPointerException{ } <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">抛出父类异常中的一部分</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method() <span style="color: rgba(0, 0, 255, 1)">throws</span> ClassCastException { } <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">抛出父类异常中的一部分</span>
    }</pre>
    </div>
    <p><span style="line-height: 1.5">3、当被覆盖的方法没有异常声明时，子类覆盖是无法声明异常的。</span></p>
    <p>例如：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Fu {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method (){
        }
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">class</span> Zi <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Fu {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method() <span style="color: rgba(0, 0, 255, 1)">throws</span> Exception { }<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">错误的方式</span>
    }</pre>
    </div>
    <p><span style="line-height: 1.5">举例：父类中会存在下列这种情况，接口也有这种情况</span></p>
    <p>问题：接口中没有声明异常，而实现的子类覆盖方法时发生了异常，怎么办？</p>
    <p>答：无法进行throws声明，只能catch的捕获。万一问题处理不了呢？catch中继续throw抛出，但是只能将异常转换成RuntimeException子类抛出。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">interface</span><span style="color: rgba(0, 0, 0, 1)"> Inter {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">abstract</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> method();
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">class</span> Zi <span style="color: rgba(0, 0, 255, 1)">implements</span><span style="color: rgba(0, 0, 0, 1)"> Inter {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> method(){ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">无法声明 throws Exception</span>
            <span style="color: rgba(0, 0, 255, 1)">int</span>[] arr = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (arr == <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
    　　　　　　　　</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">只能捕获处理</span>
                <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
                    </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Exception(“哥们，你定义的数组arr是空的!<span style="color: rgba(0, 0, 0, 1)">”);
                } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(Exception e){
                    System.out.println(“父方法中没有异常抛出，子类中不能抛出Exception异常”);
                    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">我们把异常对象e，采用RuntimeException异常方式抛出</span>
                    <span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException(e);
                }
            }
        }
    }</span></pre>
    </div>
    <h3><span style="line-height: 1.5">异常中常用方法</span></h3>
    <p>在Throwable类中为我们提供了很多操作异常对象的方法，常用的如下：&nbsp;</p>
    <ul>
    <li>getMessage方法：返回该异常的详细信息字符串，即异常提示信息</li>
    <li>toString方法：返回该异常的名称与详细信息字符串</li>
    <li>printStackTrace：在控制台输出该异常的名称与详细信息字符串、异常出现的代码位置</li>
    </ul>
    <p>异常的常用方法代码演示：</p>
    <div class="cnblogs_code">
    <pre> <span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
            Person p</span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (p==<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> NullPointerException(“出现空指针异常了，请检查对象是否为null”);
            }
        } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (NullPointerException e) {
            String message </span>=<span style="color: rgba(0, 0, 0, 1)"> e.getMesage();
            System.out.println(message );
            String result </span>=<span style="color: rgba(0, 0, 0, 1)"> e.toString();
            System.out.println(result);
            e.printStackTrace();
        }</span></pre>
    </div>
    <h2><span style="line-height: 1.5">自定义异常</span></h2>
    <p>在上述代码中，发现这些异常都是JDK内部定义好的，并且这些异常不好找。书写时也很不方便，那么能不能自己定义异常呢？</p>
    <p>之前的几个异常都是java通过类进行的描述。并将问题封装成对象，异常就是将问题封装成了对象。这些异常不好认，书写也很不方便，能不能定义一个符合我的程序要求的异常名称。既然JDK中是使用类在描述异常信息，那么我们也可以模拟Java的这种机制，我们自己定义异常的信息，异常的名字，让异常更符合自己程序的阅读。准确对自己所需要的异常进行类的描述。</p>
    <h3>自定义异常类的定义</h3>
    <p>通过阅读异常源代码：发现java中所有的异常类，都是继承Throwable，或者继承Throwable的子类。这样该异常才可以被throw抛出。说明这个异常体系具备一个特有的特性：可抛性：即可以被throw关键字操作。</p>
    <p>并且查阅异常子类源码，发现每个异常中都调用了父类的构造方法，把异常描述信息传递给了父类，让父类帮我们进行异常信息的封装。</p>
    <p>例如NullPointerException异常类源代码：</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span> NullPointerException <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> NullPointerException() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span>();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用父类构造方法</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> NullPointerException(String s) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span>(s);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">调用父类具有异常信息的构造方法</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">现在，我们来定义个自己的异常，即自定义异常。</span></p>
    <p>格式：</p>
    <div class="cnblogs_code">
    <pre>Class 异常名 <span style="color: rgba(0, 0, 255, 1)">extends</span> Exception{ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">或继承RuntimeException</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> 异常名(){
        }
    
        </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> 异常名(String s){
       　　 </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(s);
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">自定义异常继承Exception演示</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span> MyException <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Exception{
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    为什么要定义构造函数，因为看到Java中的异常描述类中有提供对异常对象的初始化方法。
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> MyException(){
        </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
    }
    </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> MyException(String message) {
        </span><span style="color: rgba(0, 0, 255, 1)">super</span>(message);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果自定义异常需要异常信息，可以通过调用父类的带有字符串参数的构造函数即可。</span>
    <span style="color: rgba(0, 0, 0, 1)">
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">自定义异常继承RuntimeException演示</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span> MyException <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> RuntimeException{
    </span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
    为什么要定义构造函数，因为看到Java中的异常描述类中有提供对异常对象的初始化方法。
    </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
        MyException(){
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        }
        MyException(String message) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span>(message);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果自定义异常需要异常信息，可以通过调用父类的带有字符串参数的构造函数即可。</span>
    <span style="color: rgba(0, 0, 0, 1)">    }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">自定义异常的练习</span></p>
    <p>定义Person类，包含name与age两个成员变量。在Person类的有参数构造方法中，进行年龄范围的判断，若年龄为负数或大于200岁，则抛出NoAgeException异常，异常提示信息“年龄数值非法”。</p>
    <p>要求：在测试类中，调用有参数构造方法，完成Person对象创建，并进行异常的处理。</p>
    <p>自定义异常类</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span> NoAgeException <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Exception{
        NoAgeException() {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">();
        }
    
        NoAgeException(String message) {
            </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(message);
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">Person类</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Person{
        </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name;
        </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> age;
        Person(String name,</span><span style="color: rgba(0, 0, 255, 1)">int</span> age) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> NoAgeException {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">加入逻辑判断。</span>
            <span style="color: rgba(0, 0, 255, 1)">if</span>(age&lt;0 || age&gt;200<span style="color: rgba(0, 0, 0, 1)">) {
                </span><span style="color: rgba(0, 0, 255, 1)">throw</span> <span style="color: rgba(0, 0, 255, 1)">new</span> NoAgeException(age+",年龄数值非法"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
            </span><span style="color: rgba(0, 0, 255, 1)">this</span>.age =<span style="color: rgba(0, 0, 0, 1)"> age;
        }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">定义Person对象对应的字符串表现形式。覆盖Object中的toString方法。</span>
        <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Person[name="+name+",age="+age+"]"<span style="color: rgba(0, 0, 0, 1)">;
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">测试类</span></p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> ExceptionDemo{
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                Person p </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> Person("xiaoming",20<span style="color: rgba(0, 0, 0, 1)">);
                System.out.println(p);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (NoAgeException ex){
                System.out.println(</span>"年龄异常啦"<span style="color: rgba(0, 0, 0, 1)">);
            }
            System.out.println(</span>"over"<span style="color: rgba(0, 0, 0, 1)">);
        }
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">总结一下，构造函数到底抛出这个NoAgeException是继承Exception呢？还是继承RuntimeException呢？</span></p>
    <p>继承Exception，必须要throws声明，一声明就告知调用者进行捕获，一旦问题处理了调用者的程序会继续执行。</p>
    <p>继承RuntimeExcpetion,不需要throws声明的，这时调用是不需要编写捕获代码的，因为调用根本就不知道有问题。一旦发生NoAgeException，调用者程序会停掉，并有jvm将信息显示到屏幕，让调用者看到问题，修正代码。</p>
    <h2>总结</h2>
    <p>异常：就是程序中出现的不正常的现象(错误与异常)</p>
    <h3>异常的继承体系</h3>
    <ul>
    <li>Throwable: 它是所有错误与异常的超类（祖宗类）</li>
    <li>|- Error 错误，修改java源代码</li>
    <li>|- Exception 编译期异常, javac.exe进行编译的时候报错</li>
    <li>|- RuntimeException 运行期异常, java出现运行过程中出现的问题</li>
    </ul>
    <h3>异常处理的两种方式</h3>
    <p>1、出现问题，自己解决 try…catch…finally</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">{
    
        可能出现异常的代码
    
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(异常类名  对象名){
    
        异常处理代码
    
    } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
    
        异常操作中一定要执行的代码
    
    }</span></pre>
    </div>
    <p><span style="line-height: 1.5">2、出现问题，别人解决 throws</span></p>
    <p>格式：修饰符 返回值类型 方法名(参数) throws 异常类名1,异常类名2,...{}</p>
    <p>比如：public void method() throws Exception{}&nbsp;</p>
    <h3>异常分类</h3>
    <p>异常的根类是Throwable，其下有两个子类：Error与Exception，平常所说的异常指Exception；严重错误Error，无法通过处理的错误。</p>
    <ul>
    <li>编译时异常Exception，编译时无法编译通过。如日期格式化异常</li>
    <li>运行时异常RuntimeException，是Exception的子类，运行时可能会报错，可以不处理。如空指针异常</li>
    </ul>
    <p><strong>异常基本操作</strong></p>
    <ul>
    <li>创建异常对象</li>
    <li>抛出异常</li>
    <li>处理异常：</li>
    </ul>
    <ol>
    <li>捕获处理，将异常获取，使用try/catch做分支处理<span style="line-height: 1.5">&nbsp;</span></li>
    <li>声明抛出处理，出现异常后不处理，声明抛出给调用者处理。<span style="line-height: 1.5">&nbsp;方法声明上加throws&nbsp; 异常类名</span></li>
    </ol>
    <p>注意：异常的处理，指处理异常的一种可能性，即有了异常处理的代码，不一定会产生异常。如果没有产生异常，则代码正常执行，如果产生了异常，则中断当前执行代码，执行异常处理代码。</p>
    <h3><span style="line-height: 1.5">多异常处理</span></h3>
    <p><strong>捕获处理</strong></p>
    <ul>
    <li>多个异常可以分别处理</li>
    <li>多个异常一次捕获多次处理</li>
    <li>多个异常一次捕获，采用同一种方式处理</li>
    </ul>
    <p>声明抛出异常：声明上使用,一次声明多个异常</p>
    <p>运行时异常被抛出可以不处理。即不捕获也不声明抛出</p>
    <p>如果父类抛出了多个异常,子类覆盖父类方法时,只能抛出相同的异常或者是他的子集</p>
    <p>父类方法没有抛出异常，子类覆盖父类该方法时也不可抛出异常。此时子类产生该异常，只能捕获处理，不能声明抛出</p>
    <p>当多异常处理时，捕获处理，前边的类不能是后边类的父类</p>
    <h3><span style="line-height: 1.5">自定义异常</span></h3>
    <p>如果Java没有提供你需要的异常，则可以自定义异常类。</p>
    <p>定义方法：编译时异常继承Exception，运行时异常继承RuntimeException。</p>
    <h3>throw 和throws 的区别是什么</h3>
    <p>throw <br>定义在方法中，后边跟的是异常对象<br>同时只能抛出一个异常对象</p>
    <p>throws <br> 定义在方法的声明上，后边跟的是异常的类型<br> 后边同时可以跟多个数据类型</p>
    <h3>finally 返回路径</h3>
    <p>finally ：在正常情况下，肯定执行的代码，在try中return，在finally中修改。每次碰到return就会在返回路径中临时存储这个被返回的值，无论方法内有任何的改变，返回路径中的这个值一致不变。</p>
    <div class="cnblogs_code">
    <pre><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Demo {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> main(String[] args) {
            Test t </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Test();
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> method =<span style="color: rgba(0, 0, 0, 1)"> t.method();
            System.out.println(method);
        }
    }
    
    </span><span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)"> Test {
        </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> method() {
            </span><span style="color: rgba(0, 0, 255, 1)">int</span> i = 0<span style="color: rgba(0, 0, 0, 1)">;
            </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
                System.out.println(</span>1 / 0<span style="color: rgba(0, 0, 0, 1)">);
                </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> i;
            } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (Exception e) {
                e.printStackTrace();
            } </span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
                i </span>= 200<span style="color: rgba(0, 0, 0, 1)">;
                System.out.println(</span>"我一定会运行"<span style="color: rgba(0, 0, 0, 1)">);
            }
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> i;
        }
        </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">返回路径：每次碰到return就会在返回路径中临时存储这个被返回的值，无论方法内有任何的改变，返回路径中的这个值一致不变。</span>
    <span style="color: rgba(0, 0, 0, 1)">}
    
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">我一定会运行
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">200
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">java.lang.ArithmeticException: / by zero
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">at inter.Test.method(Demo.java:15)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">at inter.Demo.main(Demo.java:6)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Process finished with exit code 0</span></pre>
    </div>
    <p>&nbsp;</p>
    </div>

</body>
</html>